<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('用户地址列表')" />
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css" rel="stylesheet">
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>用户名称：</label>
                                <input type="text" name="nickName"/>
                            </li>
                            <li>
                                <label style="width: 100px;">收货人姓名：</label>
                                <input type="text" name="userName"/>
                            </li>
                            <li>
                                <label style="width: 140px;">收货人联系方式：</label>
                                <input type="text" name="userPhone"/>
                            </li>
                            <li style="width: 700px;">
                                <label>收货地址：</label>
                                <div class="col-sm-8">
                                    <select class="selectpicker form-control" onchange="initCity()" id="addressProv" name="addressProv">
                                        <option value="">--省份--</option>
                                    </select>
                                    <select class="selectpicker form-control" onchange="initRegion()" id="addressCity" name="addressCity">
                                        <option value="">--城市--</option>
                                    </select>
                                    <select class="selectpicker form-control" id="addressDist" name="addressDist">
                                        <option value="">--县区--</option>
                                    </select>
                                </div>
                            </li>
                            <li>
                                <label style="width: 120px;">是否默认地址：</label>
                                <select name="isDefault" th:with="type=${@dict.getType('is_default_address')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
     <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>
    <script th:inline="javascript">
        var isDefaultDatas = [[${@dict.getType('is_default_address')}]];
        var prefix = ctx + "jjyh/useraddress";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "用户地址",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'nickName',
                    title: '用户名称'
                },
                {
                    field: 'userName',
                    title: '收货人姓名'
                },
                {
                    field: 'userPhone',
                    title: '收货人联系方式'
                },
                {
                    field: 'addressProvname',
                    title: '收货省份名称'
                },
                {
                    field: 'addressCityname',
                    title: '收货城市名称'
                },
                {
                    field: 'addressDistname',
                    title: '收货区域名称'
                },
                {
                    field: 'addressDesc',
                    title: '收货地址描述'
                },
                {
                    field: 'isDefault',
                    title: '是否默认地址',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(isDefaultDatas, value);
                    }
                }]
            };
            $.table.init(options);
        });

        $(document).ready(function(){
            // 地区下拉框初始化
            $("#addressProv").selectpicker({width:130, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#addressCity").selectpicker({width:120, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#addressDist").selectpicker({width:120, liveSearch: true, liveSearchPlaceholder: "搜索" });

            // 动态加载省份选项数据
            initSelectOptions("addressProv","0");
        });// onReady

        /**
         * 动态生成select选项
         * @param selectId
         * @returns
         */
        function initSelectOptions(selectId,parentId) {
            var selectObj = $("#" + selectId);
            $.ajax({
                url : ctx + "jjyh/goodsrelease/"+parentId,
                async : false,
                type : "GET",
                success : function(result) {

                    var configs = result.rows;
                    selectObj.find("option:not(:first)").remove();
                    for (var i in configs) {
                        var addressConfig = configs[i];
                        var optionValue = addressConfig.id;
                        var optionText = addressConfig.areaName;
                        selectObj.append(new Option(optionText, optionValue));
                    }
                    // 刷新select
                    selectObj.selectpicker('refresh');
                },
                error : function(result) {
                    toastr.error('获取['+ parentId + ']信息失败，原因：' + result.errorMessage);
                }
            });// ajax
        }

        /**
         * 根据选择的省份动态初始化城市options
         * @returns
         */
        function initCity() {
            // 当省份变动时，初始化城市和清空县区和街道
            var provinceSel = $("#addressProv").val();
            initSelectOptions("addressCity", provinceSel);
            $("#addressDist").find("option:not(:first)").remove();
            $("#addressDist").selectpicker('refresh');
        }

        /**
         * 根据选择的城市动态初始化区域options
         * @returns
         */
        function initRegion() {
            // 选择城市
            var citySel = $("#addressCity").val();
            initSelectOptions("addressDist", citySel);
        }
    </script>
</body>
</html>